/**
 * 候选人
 * 淘汰、推荐其他岗位、转发
 */

import React from 'react';
import { history } from 'umi'
import AuthComponent from '@/utils/auth/AuthComponent';
import helper from '@/utils/auth/helper';
import { sameTabProps } from '@/utils/tab';

import MoreOperateModal from './MoreOperateModal';
import { CANDIDATE_OPERATE_TYPE, NOTICE_TYPE, RECOMMEND_TYPE_VALUES } from '../../dictionary';
import { Icon } from '../../components';

const { checkCode } = helper;

// 100: checkCode('office:recruitment:course:recommend')

export default function MoreOperate({
    resumeId,
    candidateId,
    button,
    type,
    onConfirm,
    noticeType = NOTICE_TYPE.WEED_OUT,
    recommendOutDisabled,
    talentCode,
    showSelect = true,
    ...rest
}) {
    // 针对候选人的操作类型
    // 1->淘汰 2->推荐到其他岗位  3->转发 5->推荐到岗位
    const authMap = {
        1: checkCode('office:recruitment:course:out'),
        [RECOMMEND_TYPE_VALUES.OTHER]: checkCode('office:recruitment:course:recommend'),
        3: checkCode('office:recruitment:course:transfer')
    };

    let newAuthMap = JSON.parse(JSON.stringify(authMap));
    // 人才库-推荐到岗位功能要复用 推荐到其他岗位功能，服务端没有定义新的类型，前端自己定义一个新的类型，逻辑还走推荐到其他岗位逻辑
    if (talentCode) {
        newAuthMap = {
            ...authMap,
            [RECOMMEND_TYPE_VALUES.OTHER]: checkCode('office:recruitment:talentsManagement:recommend'),
            [RECOMMEND_TYPE_VALUES.COMMON]: checkCode('office:recruitment:talentsManagement:recommend')
        };
    }
    const { title, iconType, operate } = React.useMemo(() => {
        if (!type) {
            return {
                title: '更多',
                iconType: 'listfunction_more_icon_default',
                operate: Object.keys(authMap)
                    .filter(key => authMap[key])
                    .map(_ => parseInt(_, 10))
            };
        }
        const item = CANDIDATE_OPERATE_TYPE.find(_ => _.value === type) || {};
        return { title: item.text, iconType: item.iconType, operate: [type] };
    }, [type]);
    const initModalState = { title, operate, showSelect, visible: false };
    const [modalState, setModalState] = React.useState(initModalState);
    const showModal = React.useCallback(() => {
        setModalState(state => ({ ...state, visible: true, resumeId, talentCode }));
    }, [setModalState, resumeId, talentCode]);
    if (!type && Object.values(newAuthMap).every(_ => !_)) {
        return null;
    }
    return (
        <AuthComponent code={type ? newAuthMap[type] : true}>
            <Icon title={title} button={button} type={iconType} onClick={showModal} {...rest} />
            <MoreOperateModal
                {...modalState}
                recommendOutDisabled={recommendOutDisabled}
                onOk={({ emailNoticeFlag, refreshFlag }) => {
                    setModalState(() => ({
                        ...initModalState
                    }));
                    if (emailNoticeFlag) {
                        history.push({
                            pathname: '/office/personnelManagement/recruitment/candidate/notice',
                            query: { candidateId, noticeType },
                            ...sameTabProps
                        });
                    }
                    if (refreshFlag && onConfirm) {
                        onConfirm();
                    }
                }}
                onCancel={() =>
                    setModalState(() => ({
                        ...initModalState
                    }))
                }
            />
        </AuthComponent>
    );
}
